<template>
  <!-- 歌单 -->
  <div class="songList">
    <div class="checkAll">
      <el-button
        type="primary"
        size="small"
        @click="handleCheckAllChange"
        v-text="checkAll ? $t('main.deselectAll') : $t('main.selectAll')"
      ></el-button>
      <el-button
        @click="clearBtn"
        class="clearBtn"
        size="small"
        icon="el-icon-delete-solid"
        >{{ $t("public.delete") }}</el-button
      >
    </div>
    <el-checkbox-group
      v-model="checkList"
      class="list"
      v-infinite-scroll="load"
      infinite-scroll-disabled="isPage"
      infinite-scroll-delay="120"
      infinite-scroll-distance="10"
      infinite-scroll-immediate="false"
    >
      <!-- @childCheck="childCheck" -->
      <album-list
        ref="myChild"
        :listData="list"
        :loading="loading"
        :isCheck="true"
        :diff="'song-list'"
        v-loading="fullscreenLoading"
      ></album-list>
    </el-checkbox-group>
    <nice-empty
      v-show="list.length == 0 && fullscreenLoading == false"
      :emptyText="$t('public.noData')"
    ></nice-empty>
    <div class="page-wrap" v-show="isPage">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-size="params.pageSize"
        background
        hide-on-single-page
        layout="total, prev, pager, next, jumper"
        :total="total"
        :small="isMobile"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import albumList from "@/components/common/albumList/Index";
export default {
  data() {
    return {
      list: [], //列表数据
      // 是否显示加载动画
      loading: false,
      //数据载入动画
      fullscreenLoading: true,
      //是否显示分页 / 加载更多
      isPage: false,
      params: {
        // 返回数量
        pageSize: 6,
        // 当前页数
        pageNum: 1,
      },
      checkList: [], //多选 选中项
      total: 0, //总条数
    };
  },
  components: {
    albumList,
  },
  computed: {
    checkAll() {
      return this.checkList.length === this.list.length && this.list.length > 0;
    },
    isMobile() {
      return this.$store.getters.isMobile;
    },
    isIE() {
      return this.$store.getters.isIE;
    },
  },
  watch: {},
  created() {},
  mounted() {
    if (this.isIE) {
      this.params.pageSize = 14;
    }
    this.getData();
  },
  methods: {
    //子组件传 选中项
    // childCheck() {
    //   return this.checkAll;
    //   // this.checkAll = this.list.length === this.checkList.length;
    // },
    //分页 每页条数
    handleSizeChange(val) {
      this.params.pageSize = val;
      this.getData();
    },
    //当前页
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.list = [];
      this.checkList = [];
      this.getData();
    },
    async getData() {
      this.fullscreenLoading = true;
      try {
        let res = await this.$api.user.getSongSheetList(this.params);
        if (res.code === 1000000) {
          this.list = res.source ? res.source : [];
          this.total = res.total;
          this.fullscreenLoading = false;
          if (res.size < 14 && res.size < res.total) {
            this.loading = true;
            this.isPage = false;
          } else {
            this.loading = false;
            this.isPage = true;
          }
        }
      } catch (e) {
        console.log(e);
      }
    },
    //全选
    handleCheckAllChange() {
      if (this.checkAll) {
        this.checkList = [];
        // this.checkAll = false;
      } else {
        this.checkList = this.list.map((item) => {
          return item["songSheetId"];
        });
        // this.checkAll = true;
      }
      // this.$refs.myChild.isCheckAll();
    },
    // 删除 全部/部分
    clearBtn() {
      if (!this.$store.getters.loginStatu) {
        this.$notify.error({
          title: this.$t("public.sorry"),
          message: this.$t("public.loginPermission"),
          duration: 3000,
        });
        return;
      }
      if (this.checkList == 0) {
        this.$message({
          showClose: true,
          message: this.$t("public.pleaseSelectSongListToDeleteFirst"),
          type: "warning",
          duration: 1500,
        });
        return;
      }
      this.$confirm(this.$t("public.noLike"), {
        confirmButtonText: this.$t("public.confirm"),
        cancelButtonText: this.$t("public.cancel"),
        showClose: false,
        center: true,
      })
        .then(async () => {
          let params = {
            // userId: this.params.userId,
            type: 3,
            contentIds: this.checkList,
            selectState: this.checkAll ? 1 : 2,
          };
          let res = await this.$api.user.cancelSongSheet(params);
          if (res.code === 1000000) {
            this.$message({
              showClose: true,
              type: "success",
              message: this.$t("public.successfullyDelete"),
              duration: 1500,
            });
            this.getData();
            this.checkList = [];
          }
        })
        .catch((err) => {
          console.log(err);
          // this.$message({
          //   type: "info",
          //   message: "已取消操作",
          // });
        });
    },
    // 加载更多
    load() {
      // this.checkAll = false;
      this.params.pageSize += 4;
      this.getData();
    },
  },
};
</script>

<style lang="scss" scoped></style>
